package com.action;

import com.vo.ResponseBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/Emp")
@CrossOrigin("*")
public class UploadAction {
/**
 * @program: S2020-2-Spring
 *
 * @description: 测试：文件上传
 *
 * @author: Laoqi
 *
 * @create: 2019-11-08 10:51
 **/

    @Value("${upload.dir}")
    private String tmp;
    @RequestMapping("/upload")
    public ResponseBean upload(MultipartFile file) throws IOException {
        //得到原来的文件名 xxx.jpg
        String old = file.getOriginalFilename();

        //得到最后一个.的位置
        int index = old.lastIndexOf('.');

        //得到文件的后缀名
        String extName = old.substring(index);

        //新的文件名
        String newFile = System.currentTimeMillis()+extName;

        //传到tmp目录
        File descFile = new File(tmp,newFile);
        file.transferTo(descFile);

        return new ResponseBean(0,0,newFile);


    }


}
